﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;

namespace bd2wpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = dataSourceMyClass;//Привязка данных
        }

        #region События
        private void Add_Click(object sender, RoutedEventArgs e) //Добавление новой строки
        {       
            Add w1 = new Add();
            w1.setparent(this);
            w1.Show();     
        }

        private void Cansel_Click(object sender, RoutedEventArgs e)//Отмена фильтрации
        {
            dataGrid1.ItemsSource = dataSourceMyClass.myClasses;
            dataGrid1.Items.Refresh();
        }

        private void Filtr_Click(object sender, RoutedEventArgs e)//Начало фильтрации
        {
            if (FiltrCB.Text == "") { MessageBox.Show("Ошибка! Не выбранно поле фильтрации!"); }
            else { FiltrGo(); }
        }

        private void Exit_Click(object sender, RoutedEventArgs e) //Выход
        {
            this.Close();
        }

        private void Search_Click(object sender, RoutedEventArgs e)//Открытие окна поиска
        {
            Search w3 = new Search();
            w3.setparent(this);
            w3.Show();
        }

        private void Delete_Click(object sender, RoutedEventArgs e)//удаление строки
        {
            int i = dataGrid1.SelectedIndex ;
            if (i == -1) { MessageBox.Show("Выделение пусто!"); }
            else
            {
                dataSourceMyClass.myClasses.RemoveAt(i);
                dataGrid1.Items.Refresh();
            }
        }

        private void Connect_Click(object sender, RoutedEventArgs e)//соединение с сервером
        {

            Connect w2 = new Connect();
            w2.setparent(this);
            w2.Show();
        }

        private void Save_Click(object sender, RoutedEventArgs e) //Сохранение данных в БД
        {
            try
            {
                SqlCommand cmd = new SqlCommand("Contacts..SP_Delete_all_data", con);
                cmd.Connection = con;
                cmd.BeginExecuteReader();
                ListToBD();
            }
            catch (InvalidOperationException ie)
            {
                MessageBox.Show(ie.Message);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Disconnect_Click(object sender, RoutedEventArgs e)//Отключение от БД
        {
            try
            {
                con.Close();
                dataSourceMyClass.myClasses.Clear();
                dataGrid1.Items.Refresh();
                Save.IsEnabled = false;
                Add.IsEnabled = false;
                SearchB.IsEnabled = false;
                Delete.IsEnabled = false;
                SaveM.IsEnabled = false;
                Disconnect.IsEnabled = false;
                SearchM.IsEnabled = false;
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.Message);
                
            }
        }

        private void Help_Click(object sender, RoutedEventArgs e)//Открытие окна помощи
        {
            Help w3 = new Help();
            w3.Show();
        }

        private void About_Click(object sender, RoutedEventArgs e)//Открытие окна о программе
        {
            About w4 = new About();
            w4.Show();
        }
        
        #endregion
   
    }
}
